//
// Mouse Gesture Recognizer
// (c) Robert Hahn / HTW Dresden
//
// InputFunction.cs
// ----------------
// Interface and implementations of input functions.
//

using System;


namespace CNeural
{
	/// <summary>
	/// Provides an interface of an input function.
	/// </summary>
	public interface IInputFunction
	{
		/// <summary>
		/// Computes the result of the input function if implemented.
		/// </summary>
		/// <param name="input">The incoming connections from the previous layer.</param>
		/// <returns>The result value of the input function.</returns>
		double Compute(ConnectionList input);
	}

	
	/// <summary>
	/// Represents an input function which calculates the sum of all inputs.
	/// </summary>
	[Serializable]
	public class SumInputFunction : IInputFunction 
	{
		public double Compute(ConnectionList input) 
		{
			double result = 0F;
			foreach (Connection c in input) 
			{
				result += c.Output;
			}
			return result;
		}
	}


	/// <summary>
	/// Represents an input function which returns 1.0 if the majority if inputs
	/// has a value of 1.0.
	/// </summary>
	public class MajorityInputFunction : IInputFunction 
	{
		public double Compute(ConnectionList input) 
		{
			int activeCount = 0;
			foreach (Connection c in input) 
			{				
				if (c.Output == 1.0) activeCount++;
			}
			if (activeCount > (input.Count / 2)) return 1F;
			return 0F;
		}
	}
}
